package com.leaf.u_spring.chapter13;

import net.sf.ehcache.CacheManager;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

/**
 * @EnableCaching启用注解
 * 会创建一个切面(aspect)并触发Spring缓存注解的切点(pointcut)
 * 根据所使用的注解以及缓存的状态，这个切面会从缓存中获取数据，将数据添加到缓存之中或者从缓存中移除某个值
 *
 */
@Configuration
@EnableCaching		
public class CachingConfig {

	/**
	 * 声明缓存管理器  
	 * 默认数据缓存到内存中，使用ConcurrentHashMap存储
	 * ConcurrentHashMap适合开发测试或基础应用，不适合生产级别的大型企业应用
	 * @param cm
	 * @return
	 */
//	@Bean
//	public org.springframework.cache.CacheManager cacheManager() {
//		return new org.springframework.cache.concurrent.ConcurrentMapCacheManager();
//	}
	
	
	@Bean
	public EhCacheCacheManager cacheManager(CacheManager cm) {
		return new EhCacheCacheManager(cm);
	}
	
	@Bean
	public EhCacheManagerFactoryBean ehcache() {
		EhCacheManagerFactoryBean ehCacheFactoryBean = 
				new EhCacheManagerFactoryBean();
		ehCacheFactoryBean.setConfigLocation(
				new ClassPathResource("cache/ehcache.xml"));
		return ehCacheFactoryBean;
	}
  
}